import { getGeneralMessageCode, postAddSupplierMember } from '../../../api/index'
let timer = null

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    detail: {
      type: Object,
      value: {}
    }
  },
  data: {
    showTypePop: false,
    time: 0,
    form: {
      mobile: '',
      msgCode: '',
      roleId: '',
      roleStr: ''
    },
    currentItem: {},
    columns: [
      { label: '管理员', value: 2 },
      { label: '普通成员', value: 3 }
    ]
  },
  methods: {
    codeTimer () {
      this.setData({ time: 60 })
      timer = setInterval(() => {
        this.data.time--
        this.setData({ time: this.data.time })
        if (this.data.time <= 0) {
          clearInterval(timer)
        }
      }, 1000)
    },
    async sendCode () {
      try {
        let reg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/
        if (this.data.time > 0 && this.isSendCode) {
          return
        }
        if (!this.data.form.mobile || !reg.test(this.data.form.mobile)) {
          wx.showToast({ title: '请输入正确的手机号！', icon: 'none' })
          return
        }
        this.isSendCode = true
        wx.showLoading({ title: '短信发送中...', mask: true })
        await getGeneralMessageCode({ mobile: this.data.form.mobile, type: 1 })
        this.isSendCode = false
        wx.hideLoading()
        await this.codeTimer()
        wx.showToast({ title: '短信验证码已发送', icon: 'none' })
      } catch (err) {
        wx.hideLoading()
        this.isSendCode = false
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    changeInput ({ currentTarget: { dataset: { key } }, detail: { value } }) {
      this.setData({ ['form.' + key]: value })
    },
    close () {
      this.triggerEvent('close')
    },
    async confirm () {
      try {
        let reg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/
        if (!this.data.form.mobile || !reg.test(this.data.form.mobile)) {
          wx.showToast({ title: '请输入正确的手机号！', icon: 'none' })
          return
        }
        if (!this.data.form.msgCode) {
          wx.showToast({ title: '请输入验证码！', icon: 'none' })
          return
        }
        if (!this.data.form.roleId) {
          wx.showToast({ title: '请选择成员角色！', icon: 'none' })
          return
        }
        wx.showLoading({ title: '邀请中...', mask: true })
        await postAddSupplierMember({ ...this.data.form })
        wx.hideLoading()
        wx.showToast({ title: '已邀请成功！', icon: 'none' })
        this.triggerEvent('confirm')
      } catch (err) {
        wx.hideLoading()
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    select ({ currentTarget: { dataset: { item } } }) {
      this.setData({ currentItem: item })
    },
    closeTypePop () {
      clearInterval(timer)
      this.setData({ showTypePop: false, time: 0 })
    },
    async confirmType () {
      this.setData({ 'form.roleId': this.data.currentItem.value, 'form.roleStr': this.data.currentItem.label, showTypePop: false })
    },
    selectType () {
      this.setData({ showTypePop: true })
    },
    beforeOpen () {
      this.data.form = {
        mobile: '',
        msgCode: '',
        roleId: '',
        roleStr: ''
      }
      this.setData({ form: this.data.form })
    },
  },
})